"""
学习目标


概念


语法
    Base.py   基础的公共方法   在项目中由专人维护

        打开浏览器

        打开网页

        元素定位

        元素操作

        关闭浏览器

案例
"""

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from Common.GetMysqlData import Database
from selenium.webdriver.support.select import Select


def open_browser(browser: str):
    """
    封装打开浏览器的函数
    :param browser: 具体打开的浏览器 chrome firefox edge
    :return:
    """
    if browser.lower() == 'chrome':
        return webdriver.Chrome()
    elif browser.lower() == 'firefox':
        return webdriver.Firefox()
    elif browser.lower() == 'edge':
        return webdriver.Edge()

    else:
        print("输入的浏览器不合法,请输入 chrome firefox edge")
        return None


class Base(object):
    def __init__(self, browser):
        self.driver = open_browser(browser)
        self.database = Database()

    def open_url(self, url: str):
        """
        打开网页
        :param url:网页的地址,可以输入域名
        :return:
        """
        try:
            # 判断输入的网址是否是 http://或者https://开头,是就直接打开,不是就在前面加上http://
            if url.startswith('http://') or url.startswith('https://'):
                self.driver.get(url)
            else:
                url = "http://" + url
                self.driver.get(url)
        except Exception as e:
            print(f"网页打开失败,失败提示:{e}")
            print(f"输入网址是{url}")

    def user_login_client(self, username, password):
        '''登录客户端'''
        try:
            self.send_keys(("name", "username"), username)
            self.send_keys(("name", "password"), password)
            self.click(("name", "submit"))
            self.get_text_assert(expected=username, locator=("class name", "f4_b"))
        except Exception as e:
            print(f'登陆失败{e}')

    def user_login_backgroud(self, username, password):
        '''登录后台'''
        try:
            self.click(('css selector', '[onclick="btnCancel(this)"]'))
            self.click(('id', "cloudLogin"))
            self.send_keys(("name", "username"), username)
            self.send_keys(("name", "password"), password)
            self.click(('class name', 'btn-a'))
        except Exception as e:
            print(f'登陆失败{e}')

    def find_element(self, locator: tuple, timeout=20):
        """
        单个元素定位,成功返回元素,失败返回False
        :param locator: 定位器
        :param timeout: 超时时间
        :return:
        """
        try:
            return WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator))

        except Exception as e:
            print(f"元素定位失败{e}")
            return False

    def find_elements(self, locator: tuple, timeout=20):
        """
        多个元素定位,定位成功返回元素列表,失败返回False
        :param locator:定位器
        :param timeout: 超时时间
        :return:
        """
        try:
            return WebDriverWait(self.driver, timeout).until(EC.presence_of_all_elements_located(locator))
        except Exception as e:
            print(f"元素定位失败{e}")
            return False

    def send_keys(self, locator: tuple, text: str, timeout=20):
        """
        定位之后进行输入内容
        :param text: 输入的文本信息
        :param locator: 定位器
        :param timeout: 超时时间
        :return:
        """
        element = self.find_element(locator, timeout)
        if element is not False:
            element.send_keys(str(text))
        else:
            print("元素定位失败了,不能输入信息")

    def click(self, locator: tuple, timeout=20):
        """
        根据输入元素进行点击
        :param locator:
        :param timeout:
        :return:
        """
        element = self.find_element(locator, timeout)
        if element is not False:
            element.click()
        else:
            print("元素定位失败了,不能点击")

    def clear(self, locator: tuple, timeout=20):
        """
        根据输入元素进行清空
        :param locator:
        :param timeout:
        :return:
        """
        element = self.find_element(locator, timeout)
        if element is not False:
            element.clear()
        else:
            print("元素定位失败了,不能清空")

    def is_selected(self, locator: tuple, timeout=20):
        try:
            element = self.find_element(locator, timeout)
        except Exception as e:
            print(f"元素未找到{e}")
        else:
            return element.is_selected()

    def scroll_bar(self, x, y):
        """
        滚动条方法封装
        :param x: 横向移动,x初始为0
        :param y: 纵向移动,y初始为0
        :return:
        """
        js = f"window.scrollTo({x},{y})"
        self.driver.execute_script(js)

    def get_element_text(self, locator: tuple, timeout=20):
        """获取元素的文本信息"""
        element = self.find_element(locator, timeout)
        if element is not False:
            return element.text
        else:
            return None

    def get_text_assert(self, expected, locator: tuple, timeout=20):
        """
        获取文本信息进行断言
        :param expected: 预期结果
        :param locator: 定位器
        :param timeout: 超时时间
        :return: 预期内容和文本相等返回 True  不同返回False
        """
        text_content = self.get_element_text(locator, timeout)
        if text_content is not None:
            if expected == text_content:
                return True
            else:
                return False
        else:
            return False

    def get_database_data_all(self, sql):
        """获取数据库的数据"""
        try:
            data = self.database.get_all(sql)
        except Exception as e:
            print(f"从数据库-获取数据失败{e},sql语句:{sql}")
            return None
        else:
            return data

    def get_database_data_one(self, sql):
        """获取数据库的一条数据"""
        try:
            data = self.database.get_one(sql)
        except Exception as e:
            print(f"从数据库-获取数据失败{e},sql语句:{sql}")
            return None
        else:
            return data


    def clear_database_table(self, table, where=''):
        '''清空数据库表格数据'''
        sql = f'delete from {table} {where}'
        self.database.execute_sql(sql)

    def select_option(self, locator: tuple, text: str):
        '''
        下拉框选择器
        :param locator: 下拉框定位器
        :param text: 按照文本选择，传入文本
        :return:
        '''
        Select(self.find_element(locator)).select_by_visible_text(text)

    def get_text_assert_in(self, expected, locator: tuple, timeout=20):
        """
        获取文本信息进行断言
        :param expected: 预期结果
        :param locator: 定位器
        :param timeout: 超时时间
        :return: 预期内容在获取内容里面返回 True  不同返回False
        """
        text_content = self.get_element_text(locator, timeout)
        if text_content is not None:
            if expected in text_content:
                return True
            else:
                return False
        else:
            return False

    def send_keys_tab(self, locator: tuple, timeout=20):
        """
        键入tab
        """
        element = self.find_element(locator, timeout)
        if element is not False:
            element.send_keys(Keys.TAB)
        else:
            print("元素定位失败了,不能输入信息")

    def close_browser(self):
        self.driver.quit()


# if __name__ == '__main__':
    # open_browser('chrome')
    # # open_browser("Edge")
    # bs = Base('chrome')
    # # bs.open_url('http://localhost:8080/ecshop/user.php')
    # sql = 'select * from ecs_users'
    # print(bs.get_database_data_all(sql))

    # user_locator = ("name", "username")
    # password_locator = ("name", "password")
    # login_locator = ("name", "submit")
    # admin_locator = ("class name", "f4_b")
    #
    # # 输入账号
    # bs.send_keys(locator=user_locator, text='admin2')
    # # 输入密码
    # bs.send_keys(locator=password_locator, text='123456')
    # # 点击登录
    # bs.click(locator=login_locator)
    #
    # from selenium import webdriver
    #
    # driver = webdriver.Chrome()
    # driver.find_element_by_name('remember').is_selected()
    # mv1 = Base('chrome')
    # # mv1.open_url('http://localhost:8080/ecshop/user.php')
    # # mv1.user_login('fuyu57259','123456')
    # where = 'where id = 1'
    #
    # mv1.clear_database_table('ecs_cart', where)
